package com.song.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.song.common.enums.BusinessType;
import com.song.common.result.R;
import com.song.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.song.framework.annotation.RateLimiter;
import com.song.module.admin.entity.ApUser;
import com.song.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.song.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.service.IApUserService;
import com.song.module.admin.request.ApUserRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户控制层
 *
 */
@Slf4j
@RestController
@Api(value = "用户接口管理", tags = "用户接口管理")
@RequestMapping("/admin/apUser")
public class ApUserController  {


    @Resource
    private IApUserService iApUserService;


    /**
     * 分页查询用户列表
     */
    @ApiOperation(value = "分页查询用户列表")
    @PostMapping("/pageList")
    public R<PageInfo<ApUser>> pageList(@RequestBody ApUserRequest apUserRequest) {
        return R.success(iApUserService.pageList(apUserRequest));
    }

    /**
     * 获取用户详细信息
     */
    @ApiOperation(value = "获取用户详细信息")
    @GetMapping(value = "/query/{id}")
    public R<ApUser> detail(@PathVariable("id") Long id) {
        return R.success(iApUserService.getById(id));
    }

    /**
     * 新增用户
     */
    @Log(title = "新增用户", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public R<Void> add(@RequestBody ApUserRequest apUserRequest) {
        iApUserService.add(apUserRequest);
        return R.success();
    }

    /**
     * 修改用户
     */
    @Log(title = "修改用户", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody ApUserRequest apUserRequest) {
        iApUserService.update(apUserRequest);
        return R.success();
    }

    /**
     * 删除用户
     */
    @Log(title = "删除用户", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iApUserService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
     * 导出用户
     *
     * @param response response
     * @throws IOException IOException
     */
    @ApiOperation(value = "导出用户")
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response, @RequestBody ApUserRequest apUserRequest) throws IOException {
        List<ApUser> list = iApUserService.list(apUserRequest);
        ExcelUtils.exportExcel(list, ApUser.class, "用户信息", response);
    }



}
